(function() {
    'use strict';
    var kwd = SESSION.keywords;
    var newVersion = SESSION.newVersion != 1;
    var rkRoute = seajs.data.router = {
        //PAGE
        '/index.action': {
            title: '首页',
            scripts: ['page/js/index/index']
        },
        '/search.action': {
            title: '搜索',
            scripts: ['page/js/search/search']
        },
        '/usersetting.action': {
            title: '个人设置',
            scripts: ['page/js/usersetting/usersetting']
        },
        '/syssetting.action': {//????
            title: '系统设置',
            scripts: ['page/js/syssetting/syssetting']
        },
        '/score.action': {
            title: '积分规则',
            scripts: ['page/js/score/score']
        },
        '/feedback.action': {
            title: '意见反馈',
            scripts: ['page/js/feedback/feedback']
        },

        //OA
        '/final.action': {//????
            title: '详情页',
            scripts: ['page/js/final/final']
        },
        '/noticeatme.action': {
            title: '首页',
            scripts: ['oa/js/notice/notiAtmeListCtrl']
        },

        //CRM新版重构
        '/account2.action': {
            title: kwd.account,
            scripts: ['oldcrm/js/apps/account/account']
        },
         '/opportunity2.action': {
             title: kwd.opportunity,
             scripts: ['oldcrm/js/apps/opportunity/opportunity']
         },
        // '/contact2.action': {
        //     title: kwd.contact,
        //     scripts: ['oldcrm/js/apps/contact/contact']
        // },
        // '/lead2.action': {
        //     title: kwd.lead,
        //     scripts: ['oldcrm/js/apps/lead/lead']
        // },
         '/highsea2.action': {
             title: kwd.highsea,
             scripts: ['oldcrm/js/apps/highsea/highsea']
         },
        // '/leadhighsea2.action': {
        //     title: kwd.leadhighsea,
        //     scripts: ['oldcrm/js/apps/leadhighsea/leadhighsea']
        // },
        // '/campaign2.action': {
        //     title: kwd.campaign,
        //     scripts: ['oldcrm/js/apps/campaign/campaign']
        // }, 
        // '/expense2.action': {
        //     title: kwd.expense,
        //     scripts: ['oldcrm/js/apps/expense/expense']
        // },
        // '/expenseaccount2.action': {
        //     title: kwd.expenseaccount,
        //     scripts: ['oldcrm/js/apps/expenseaccount/expenseaccount']
        // },
        // '/activityrecord2.action': {
        //     title: kwd.activityrecord,
        //     scripts: ['oldcrm/js/apps/activityrecord/activityrecord']
        // },
        // '/case2.action': {
        //     title: kwd['case'],
        //     scripts: ['oldcrm/js/apps/case/case']
        // },
        // '/competitor2.action': {
        //     title: kwd.competitor,
        //     scripts: ['oldcrm/js/apps/competitor/competitor']
        // },
        // '/contract2.action': {
        //     title: kwd.contract,
        //     scripts: ['oldcrm/js/apps/contract/contract']
        // },
        // '/order2.action': {
        //     title: kwd.order,
        //     scripts: ['oldcrm/js/apps/order/order']
        // },
        // '/partner2.action': {
        //     title: kwd.partner,
        //     scripts: ['oldcrm/js/apps/partner/partner']
        // },
        // '/product2.action': {
        //     title: kwd.product,
        //     scripts: ['oldcrm/js/apps/product/product']
        // },
        // '/solution2.action': {
        //     title: kwd.solution,
        //     scripts: ['oldcrm/js/apps/solution/solution']
        // },
        // '/agent2.action': {
        //     title: kwd.agent,
        //     scripts: ['oldcrm/js/apps/agent/agent']
        // },
        // '/terminal2.action': {
        //     title: kwd.terminal,
        //     scripts: ['oldcrm/js/apps/terminal/terminal']
        // },
        '/customize2.action': {
            title: kwd.customize,
            scripts: ['oldcrm/js/apps/customize/customize']
        },
        // '/payment2.action': {
        //     title: kwd.payment,
        //     scripts: ['oldcrm/js/apps/payment/payment']
        // },
        //CRM详情
        '/account_detail.action': {
            title: kwd.account,
            scripts: ['crm/js/apps/account/detail/widget']
        },
        '/customize_detail.action': {
            title: '',
            scripts: ['crm/js/apps/customize/detail/widget']
        },
        '/opportunity_detail.action': {
            title: kwd.opportunity,
            scripts: ['crm/js/apps/opportunity/detail/widget']
        },
        //日程二级列表
        '/schedule.action': {
            title: kwd.task + kwd.schedule,
            scripts: ['oa/js/schedule/schedule']
        },
        '/doc.action': {
            title: '文档',
            scripts: ['oa/js/rescenter/newlist/rescenter']
        },
        '/opportunity_product.action':{
            title : '',
            scripts : ['crm/js/apps/product/opportunityproduct']
        },
        '/approval_list.action': {
            title: '审批',
            scripts: ['oa/js/approval/newlist/approval']
        },
        //CRM
        '/account.action': {
            title: kwd.account,
            scripts: newVersion ? ['crm/js/apps/account/account'] : ['oldcrm/js/apps/account/account']
        },
        '/campaign.action': {
            title: kwd.campaign,
            scripts: newVersion ? ['crm/js/apps/campaign/campaign'] : ['oldcrm/js/apps/campaign/campaign']
        },
        '/opportunity.action': {
            title: kwd.opportunity,
            scripts: newVersion ? ['crm/js/apps/opportunity/opportunity'] : ['oldcrm/js/apps/opportunity/opportunity']
        },
        '/contact.action': {
            title: kwd.contact,
            scripts: newVersion ? ['crm/js/apps/contact/contact'] : ['oldcrm/js/apps/contact/contact']
        },
        '/lead.action': {
            title: kwd.lead,
            scripts: newVersion ? ['crm/js/apps/lead/lead'] : ['oldcrm/js/apps/lead/lead']
        },
        '/highsea.action': {
            title: kwd.highsea,
            scripts: newVersion ? ['crm/js/apps/highsea/highsea'] : ['oldcrm/js/apps/highsea/highsea']
        },
        '/leadhighsea.action': {
            title: kwd.leadhighsea,
            scripts: newVersion ? ['crm/js/apps/leadhighsea/leadhighsea'] : ['oldcrm/js/apps/leadhighsea/leadhighsea']
        },
        '/expense.action': {
            title: kwd.expense,
            scripts: newVersion ? ['crm/js/apps/expense/expense'] : ['oldcrm/js/apps/expense/expense']
        },
        '/expenseaccount.action': {
            title: kwd.expenseaccount,
            scripts: newVersion ? ['crm/js/apps/expenseaccount/expenseaccount'] : ['oldcrm/js/apps/expenseaccount/expenseaccount']
        },
        '/activityrecord.action': {
            title: kwd.activityrecord,
            scripts: newVersion ? ['crm/js/apps/activityrecord/activityrecord'] : ['oldcrm/js/apps/activityrecord/activityrecord']
        },
        '/case.action': {
            title: kwd['case'],
            scripts: newVersion ? ['crm/js/apps/case/case'] : ['oldcrm/js/apps/case/case']
        },
        '/competitor.action': {
            title: kwd.competitor,
            scripts: newVersion ? ['crm/js/apps/competitor/competitor'] : ['oldcrm/js/apps/competitor/competitor']
        },
        '/contract.action': {
            title: kwd.contract,
            scripts: newVersion ? ['crm/js/apps/contract/contract'] : ['oldcrm/js/apps/contract/contract']
        },
        '/customize.action': {
            title: '',
            scripts: newVersion ? ['crm/js/apps/customize/customize'] : ['oldcrm/js/apps/customize/customize']
        },
        '/dashboard.action': {
            title: kwd.dashboard,
            scripts: ['oldcrm/js/apps/dashboard/dashboard']
        },
        '/forecast.action': {
            title: kwd.forecast,
            scripts: ['oldcrm/js/apps/forecast/forecast']
        },
        '/order.action': {
            title: kwd.order,
            scripts: newVersion ? ['crm/js/apps/order/order'] : ['oldcrm/js/apps/order/order']
        },
        '/partner.action': {
            title: kwd.partner,
            scripts: newVersion ? ['crm/js/apps/partner/partner'] : ['oldcrm/js/apps/partner/partner']
        },
        '/product.action': {
            title: kwd.product,
            scripts: newVersion ? ['crm/js/apps/product/product'] : ['oldcrm/js/apps/product/product']
        },
        '/goal.action': {
            title: kwd.goal,
            scripts: ['oldcrm/js/apps/salesgoal/salesgoal']
        },
        '/solution.action': {
            title: kwd.solution,
            scripts: newVersion ? ['crm/js/apps/solution/solution'] : ['oldcrm/js/apps/solution/solution']
        },
        '/report.action': {
            title: kwd.report,
            scripts: ['crm/js/apps/report/report']
        },
        '/agent.action': {
            title: kwd.agent,
            scripts: ['oldcrm/js/apps/agent/agent']
        },
        '/terminal.action': {
            title: kwd.terminal,
            scripts: ['oldcrm/js/apps/terminal/terminal']
        },
        '/salesanalysis.action': {
            title: kwd.salesanalysis,
            scripts: ['oldcrm/js/apps/salesanalysis/salesanalysis']
        },
        '/attendance.action': {
            title: kwd.attendance,
            scripts: ['oldcrm/js/apps/attendance/attendanceRecord']
        },
        '/payment.action': {
            title: kwd.payment,
            scripts: newVersion ? ['crm/js/apps/payment/payment'] : ['oldcrm/js/apps/payment/payment']
        },
        '/visit.action': {
            title: kwd.visit,
            scripts: ['oldcrm/js/apps/visit/visit']
        },
        '/attendancerecord.action': {
            title: kwd.attendance,
            scripts: ['oldcrm/js/apps/attendance/attendanceRecord']
        },
        '/attendancestatistics.action': {
            title: kwd.attendance,
            scripts: ['oldcrm/js/apps/attendance/attendanceStatistics']
        },
        //final
        '/final/account.action': {
            title: kwd.account,
            scripts: ['crm/js/apps/account/detail/widget']
        },
        '/final/customize.action': {
            title: '',
            scripts: ['crm/js/apps/customize/detail/widget']
        },
        '/final/agent.action': {
            title: kwd.agent,
            scripts: ['oldcrm/js/apps/agent/agent.detail'],
            args: ['agentDetail', 'customerid']
        },
        '/final/terminal.action': {
            title: kwd.terminal,
            scripts: ['oldcrm/js/apps/terminal/terminal.detail'],
            args: ['terminalDetail', 'customerid']
        },
        '/final/visit.action': {
            title: kwd.visit,
            scripts: ['oldcrm/js/apps/visit/visit.detail'],
            args: ['customizeDetail', 'instanceId']
        },
        '/final/activityrecord.action': {
            title: kwd.activityrecord,
            scripts: ['oldcrm/js/apps/activityrecord/activityrecord.detail'],
            args: ['activityrecordDetail', 'activityrecordId']
        },
        '/final/campaign.action': {
            title: kwd.campaign,
            scripts: ['oldcrm/js/apps/campaign/campaign.detail'],
            args: ['campaignDetail', 'campaignId']
        },
        '/final/case.action': {
            title: kwd['case'],
            scripts: ['oldcrm/js/apps/case/case.detail'],
            args: ['caseDetail', 'caseId']
        },
        '/final/competitor.action': {
            title: kwd.competitor,
            scripts: ['oldcrm/js/apps/competitor/competitor.detail'],
            args: ['customerDetail', 'customerid']
        },
        '/final/contact.action': {
            title: kwd.contact,
            scripts: ['oldcrm/js/apps/contact/contact.detail'],
            args: ['contactDetail', 'contactid']
        },
        '/final/contract.action': {
            title: kwd.contract,
            scripts: ['oldcrm/js/apps/contract/contract.detail'],
            args: ['contractDetail', 'contractid']
        },
        '/final/expense.action': {
            title: kwd.expense,
            scripts: ['oldcrm/js/apps/expense/expense.detail'],
            args: ['expenseDetail', 'instanceId']
        },
        '/final/expenseaccount.action': {
            title: kwd.expenseaccount,
            scripts: ['oldcrm/js/apps/expenseaccount/expenseaccount.detail'],
            args: ['expenseAccountDetail', 'instanceId']
        },
        '/final/lead.action': {
            title: kwd.lead,
            scripts: ['oldcrm/js/apps/lead/lead.detail'],
            args: ['leadDetail', 'customerid']
        },
        '/final/opportunity.action': {
            title: kwd.opportunity,
            scripts: ['crm/js/apps/opportunity/detail/widget'],
            args: ['opportunityDetail', 'opportunityid']
        },
        '/final/order.action': {
            title: kwd.order,
            scripts: ['oldcrm/js/apps/order/order.detail'],
            args: ['orderDetail', 'orderId']
        },
        '/final/partner.action': {
            title: kwd.partner,
            scripts: ['oldcrm/js/apps/partner/partner.detail'],
            args: ['partnerDetail', 'customerid']
        },
        '/final/product.action': {
            title: kwd.product,
            scripts: ['oldcrm/js/apps/product/product.detail'],
            args: ['productDetail', 'productid']
        },
        '/final/solution.action': {
            title: kwd.solution,
            scripts: ['oldcrm/js/apps/solution/solution.detail'],
            args: ['solutionDetail', 'solutionId']
        },

        '/final/feed.action': {
            title: kwd.feed,
            scripts: ['core/apps/feed/feed']
        },
        '/final/depart.action': {
            title: kwd.department,
            scripts: ['oa/js/colleague/colleagueDepartContentCtrl']
        },
        '/final/blog.action': {
            title: '博客',
            scripts: ['oa/js/blog/blogContentCtrl']
        },
        '/final/file.action': {
            title: '文档',
            scripts: ['oa/js/rescenter/detail/rescenterDetailCtrl']
        },
        '/final/schedule.action': {
            title: kwd.schedule,
            scripts: ['oa/js/schedule/scheduleFinalCtrl']
        },
        '/final/user.action': {
            title: "个人主页",
            scripts: ['oa/js/colleague/colleagueContentCtrl']
        },
        '/final/group.action': {
            title: kwd.group,
            scripts: ['oa/js/group/groupContentCtrl']
        },
        '/final/task.action': {
            title: kwd.task,
            scripts: ['oa/js/task/taskContentCtrl']
        },
        '/final/approval.action': {
            title: kwd.approval,
            scripts: ['oa/js/approval/approvalContentCtrl']
        },
        '/final/topic.action': {
            title: '话题',
            scripts: ['core/apps/feed/topic']
        },
        '/final/visitplan.action': {
            title: '拜访计划',
            scripts: ['oa/js/schedule/visit/visitPlanFinalCtrl']
        },
        '/workreport.action': {
            title: kwd.workreportstatistic,
            scripts: ['oa/js/workreport/statistic/workreportStatisticCtrl']
        },
        '/visitcollect.action': {
            title: kwd.visitcollect,
            scripts: ['oldcrm/js/apps/visit/visitcollect']
        },
        '/wxreport.action': {
            title: '万学精确营销管理模型',
            scripts: ['oa/js/wanxue/markingReportCtrl']
        },
        '/trackanalysis.action': {
            title: '轨迹分析',
            scripts: ['oldcrm/js/apps/trackanalysis/trackanalysis']
        },
        '/checkinmap.action': {
            title: '签到地图',
            scripts: ['oldcrm/js/apps/checkinmap/checkinmap']
        },
        '/designer.action': {
            title: '详情页面布局编辑器',
            scripts: ['designer/init']
        }
    };
    var path = location.pathname.toLowerCase(),
        cfg = rkRoute[path],
        requires = cfg ? cfg.scripts : [''],
        actionName = seajs.data.actionName = path.split('/').pop().split('.')[0].toLowerCase(),
        isFinal = seajs.data.isFinal = path.slice(0, 7) == "/final/",
        isCRMFinal = seajs.data.isCRMFinal = isFinal && cfg.args,
        //srcs = ['oldcrm/js/core/common-crm', 'rk', 'page/js/frame/pageMainCtrl'].concat(requires);
        srcs = [ 'rk'].concat(requires);

// 并发加载模块 srcs里的多个模块 ，并在都加载完成时，执行指定回调
    seajs.use(srcs, function(_, rk, pageMainCtrl, main) {

        if (rk.crm.shouldActAsDetail()) {
            isFinal = false;
            isCRMFinal = false;
        }

        function goError(code) {
            seajs.use(['page/html/404.tpl'], function(html404) {
                $('#crm-panel,#rk-panel,#oa-panel').filter(':visible:first').empty().html(html404);
            });
        }

        function onDomReady() {
            if (!cfg) {
                return goError('404');
            }            
            if(window.SESSION.prj == 'designer'){
                designerDomReady()
            }else{
                webDomReady();
            }
        }
        function designerDomReady() {
            main && main.init && main.init();
        }
        function webDomReady() {

            if (path != '/index.action') { // && !isFinal
                $('#pagecontent').find('.page-panel-inner').removeClass('page-panel-inner');
                $('#pagecontent').find('.rk-feedpanel-overflow').removeClass('rk-feedpanel-overflow');
            }

            pageMainCtrl.init && pageMainCtrl.init();
            rk.setTitle(cfg.title);

            var id = $.query('id'),
                gid = $.query('gid'),
                $panel, rkContent;
            if (isCRMFinal) {
                $panel = rk.showPageView('crm');
                $panel.addClass('detail_left_panel');
                if (id || gid) {
                    main && main.init && main.init();
                    $('<div><a href="javascript:;"></a></div>').hide().appendTo('body').detailPopup({
                        widgetName: cfg.args[0],
                        selector: 'a',
                        target: '#div_detail_frame',
                        isFinal: true
                    }).find('a').attr(id ? cfg.args[1] : 'groupid', id ? id : gid).trigger('click');
                }
            } else if (isFinal && requires.length && requires[0]) {
                $panel = rk.showPageView('rk');
                var name = path.split('/').pop().split('.')[0],
                    idMap = {
                        'feed': 'id',
                        'depart': 'gid',
                        'blog': 'bid',
                        'file': 'fid',
                        'schedule': 'scheduleId',
                        'user': 'uid',
                        'group': 'gid',
                        'task': 'taskId',
                        'approval': 'applyId',
                        'visitplan': 'planId',
                        'account': 'id',
                        'customize': 'id'
                    };
                // if (name == 'feed') {
                //     main.init($panel, id);
                // } else 

                if (idMap[name]) {
                    gid = $.query(idMap[name]);
                    if (name != 'account' || name != 'customize') {
                        $panel.html("<div class='final-panel'><div class='final-panel-inner oa-content'><div class='oa-content-detail'></div></div></div>");
                        $panel.find(".final-panel").beScroll();
                    }
                    rkContent = $panel.find(".oa-content-detail:first");
                    main.init(rkContent, gid || id);
                } else {
                    goError('404');
                }
            } else {
                main && main.init && main.init();
                //window.angular && angular.bootstrap(document, ['crmApp']);
            }
            //用户头像
            if(!rk.isEditMode)$('body').UCard();
            //页面标题
        };

        if (seajs.data.dev) {
            window.__$ = $;
        }
        var args = $.makeArray(arguments).slice(1, -1);
        $(document).ready(onDomReady);
    });
})();
